package com.asg.guesswhat;

//import net.asg.guesswho.RowColMaxExceed;
import com.asg.guesswhat.errors.RowColMaxExceedException;

/**
 * 	The GameBoard class It sets up the board a player will use to
 *	keep track of the state of their guesses
 * 
 * 	This class uses a single array to build the board The width and
 * 	height however are tracked to simulate a 2D array width is
 *	synonymous of row and height is synonymous of column
 *
 * @author blakbro2k
 * 

 */

public class GameBoard {
	/** The width of the board */
	protected int width;
	/** The height of the board */
	protected int height;
	/** The slots to hold the game cards */
	protected Card[] board;
	/** matrix to determine if card is visible or not */
	protected boolean[] isSlotVisible;

/**
 * 	 
 * Initializes a <code>GameBoard</code> instance Sets the height and width of game
 * board Sets the card set that is used on the game board Will throw an
 * error if the height times the width exceeds the Maximum card set
 * number The cards are put into the slots in the order of the card set
 * 
 * All slots are hidden by default
 * 
 * @param h The height of the board
 * @param w The width of the board
 * @param cardSet the Set of cards to use. Class <code>CardSet</code>
 * @throws RowColMaxExceedException
 */
	public GameBoard(int h, int w, CardSet cardSet) throws RowColMaxExceedException {
		if (w * h > cardSet.getMaxCardLimit()) {
			throw new RowColMaxExceedException();
		} else {
			// this.cardSet = cardSet;
			this.width = w;
			this.height = h;
			setGameBoard(cardSet);
			hideAll();
		}
	}
	
	public void setGameBoard(CardSet cardSet){
		this.board = new Card[cardSet.getMaxCardLimit()];
		this.isSlotVisible = new boolean[cardSet.getMaxCardLimit()];
		for (int i = 0; i < cardSet.getMaxCardLimit(); i++) {
			this.board[i] = cardSet.getNextCard();
			//this.board[i] = cardSet.getNextCard();

			//Gdx.app.debug("MyTag", "my error message" + cardSet.getNextCard());
		}
	}
	
	public void removeGameBoard(){
		
	}

	/**
	 * Makes the specified slot card visible
	 * 
	 * @param s
	 * 
	 */
	public void showSlot(int s) {
		isSlotVisible[s] = true;
	}

	/**
	 * Hides the specified slot card
	 * 
	 * @param s
	 * 
	 */
	public void hideSlot(int s) {
		isSlotVisible[s] = false;
	}

	/**
	 * Shows all slot cards
	 */
	public void showAll() {
		for (int i = 0; i < board.length; i++) {
			isSlotVisible[i] = true;
		}
	}

	/**
	 * Hides all slot cards
	 */
	public void hideAll() {
		for (int i = 0; i < board.length; i++) {
			isSlotVisible[i] = false;
		}
	}

	/**
	 * Returns the specified card slot if visible, returns null if not
	 * visible
	 * 
	 * @param s
	 * @return
	 */
	public Card viewSlot(int s) {
		if (isSlotVisible[s]) {
			return board[s];
		} else {
			return null;
		}
	}

	/**
	 * Sets the specified slot with a card
	 * 
	 * @param s
	 * @param card
	 */
	public void setSlot(int s, Card card) {
		board[s] = card;
	}

	/**
	 * Removes the specified slot card
	 * 
	 * @param s
	 */
	public void removeSlot(int s) {
		board[s] = null;
	}

	/**
	 * Sets a specified row
	 * 
	 * @param r
	 */
	public void setSlotRow(int r) {
	}

	/**
	 * Removes a specified row
	 * 
	 * @param r
	 */
	public void removeSlotRow(int r) {
	}

	/**
	 * Override toString to display a simple text version of the game
	 */
	public String toString() {
		String boardText = "";
		boardText += 
				"\n*_________________________________________*\n";
		int tempw = 0;
		for (int i = 0; i < board.length; i++) {
			if (tempw == width) {
				tempw = 0;
				boardText += "\n\n";
			}
			if (isSlotVisible[i]) {
				boardText += board[i].name + "\t";
			} else {
				boardText += "Hidden\t";
			}
			tempw++;
		}
		boardText += "\n*_________________________________________*\n";
		return boardText;
	}
}